﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using TGBTech.Core.Models.ChungTu;
using TGB.Core.Infrastructure;
using PagedList;
using System.Globalization;

namespace TGBTech.WebAdmin.Controllers
{
    //[Authorize]
    public class DataController : Controller
    {
        private TGBTechDb db = new TGBTechDb();
        private const int pageSize = 100;

        // GET: /Data/
        public ActionResult Index(int? page, string searchString)
        {
            if (!String.IsNullOrEmpty(searchString))
            {
                var list = db.Data.Where(p => p.SoCT.ToUpper().Contains(searchString.ToUpper()));

                ViewBag.List = list.Include(d => d.KhachHang).Include(d => d.TaiKhoanCo).Include(d => d.TaiKhoanNo)
                    .OrderBy(p => p.NgayCT).ToPagedList(1, pageSize);
            }
            else
            {
                int index = db.Data.Count() / pageSize + 1;
                int pageNumber = (page ?? index);
                ViewBag.List = db.Data.Include(d => d.KhachHang).Include(d => d.TaiKhoanCo).Include(d => d.TaiKhoanNo)
                    .OrderBy(p => p.NgayCT).ToPagedList(pageNumber, pageSize);
            }

            var tk = db.TaiKhoan.Select(p => new
                {
                    Id = p.Id,
                    Display = p.Id + " | " + p.Name
                }).ToList();

            var kh = db.KhachHang.Select(p => new
            {
                Id = p.Id,
                Display = p.Id + " | " + p.Name
            }).ToList();

            ViewBag.KhachHangId = new SelectList(kh, "Id", "Display");
            ViewBag.TaiKhoanCoId = new SelectList(tk, "Id", "Display");
            ViewBag.TaiKhoanNoId = new SelectList(tk, "Id", "Display");

            return View();
        }

        // POST: /Data/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="Id,SoCT,NgayCT,TaiKhoanNoId,TaiKhoanCoId,DienGiaiNo,KhachHangId,LoaiKH,SoTien,SoHieu,SoHoaDon,NgayHoaDon,DanhMuc,VAT")] Data data)
        {
            if (ModelState.IsValid)
            {
                data.VAT /= 100;
                db.Data.Add(data);
                db.SaveChanges();

                string khNo = "";
                string khCo = "";

                if (data.LoaiKH == true)
                {
                    khNo = data.KhachHangId;
                }
                else
                {
                    khCo = data.KhachHangId;
                }


                return Json(new { SoCT = data.SoCT,
                    NgayCT = data.NgayCT.Value.ToString("dd/MM/yyyy"),
                    No = data.TaiKhoanNoId,
                    Co = data.TaiKhoanCoId,
                    DienGiaiNo = data.DienGiaiNo,
                    DienGiaiCo = db.TaiKhoan.Where(p=>p.MaTaiKhoan == data.TaiKhoanCoId).ToList()[0].Name,
                    KHNo = khNo,
                    KHCo = khCo,
                    SoTien = data.SoTien.ToString("#,#.00#;(#,#.00#)"),
                    SoHieu = data.SoHieu,
                    SoHD = data.SoHoaDon,
                    NgayHD = data.NgayHoaDon.Value.ToString("dd/MM/yyyy"),
                    MatHang = data.DanhMuc,
                    VAT = (data.SoTien * (Decimal)data.VAT).ToString("#,#.00#;(#,#.00#)"),
                    ThueSuat = data.VAT * 100
                });
            }
            
            return View("Index", data);
        }

        // GET: /Data/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Data data = db.Data.Find(id);
            if (data == null)
            {
                return HttpNotFound();
            }

            var tk = db.TaiKhoan.Select(p => new
            {
                Id = p.Id,
                Display = p.Id + " | " + p.Name
            }).ToList();

            var kh = db.KhachHang.Select(p => new
            {
                Id = p.Id,
                Display = p.Id + " | " + p.Name
            }).ToList();

            ViewBag.KhachHangId = new SelectList(kh, "Id", "Display");
            ViewBag.TaiKhoanCoId = new SelectList(tk, "Id", "Display");
            ViewBag.TaiKhoanNoId = new SelectList(tk, "Id", "Display");

            return PartialView(data);
        }

        // POST: /Data/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include="Id,SoCT,NgayCT,TaiKhoanNoId,TaiKhoanCoId,DienGiaiNo,KhachHangId,LoaiKH,SoTien,SoHieu,SoHoaDon,NgayHoaDon,DanhMuc,VAT")] Data data)
        {
            if (ModelState.IsValid)
            {
                db.Entry(data).State = EntityState.Modified;
                db.SaveChanges();
            }

            return RedirectToAction("Index");
        }

        // GET: /Data/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Data data = db.Data.Find(id);
            if (data == null)
            {
                return HttpNotFound();
            }
            return PartialView(data);
        }

        // POST: /Data/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Data data = db.Data.Find(id);
            db.Data.Remove(data);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
